/*
 * RecordMediaPlayer.cpp
 *
 *  Created on: 2018年8月2日
 *      Author: zhengboyuan
 */
#include "stdafx.h"
#include "RecordMediaPlayer.h"
#include "CLog.h"


RecordMediaPlayer::RecordMediaPlayer():
	m_duration()
{

}

RecordMediaPlayer::~RecordMediaPlayer()
{
}

bool RecordMediaPlayer::CanSeek()
{
    return true;
}

bool RecordMediaPlayer::Open(nvs::NetVideoSystem* system, const nvs::RecordItem& rec)
{
    if (!system)
    {
        return false;
    }

	memcpy(&m_rec, &rec, sizeof(rec));
   // m_rec = rec;

    nvs::MediaStream* stream = NULL;
    int ret = system->openRecordStream(rec, &stream);
    if (ret != nvs::NVS_ERR_OK)
    {
		CLog::warning("RecordMediaPlayer::Open. failed to open %s from %lld to %lld. ret: %d\n", rec.id, rec.beginTime, rec.endTime, ret);
        return false;
    }

	CLog::info("RecordMediaPlayer::Open. dev: %s from %lld to %lld. openRecordStream 0x%p\n", rec.id, rec.beginTime, rec.endTime, stream);

	m_duration = (int)(rec.endTime - rec.beginTime);

    return MediaStreamPlayer::Open(system, stream);
}

const nvs::RecordItem& RecordMediaPlayer::getRecord() const
{
    return m_rec;
}

std::string RecordMediaPlayer::GetUrl()
{
    return m_rec.id;
}

int64_t RecordMediaPlayer::GetTime()
{
	if (!m_stream)
	{
		return 0;
	}

	int64_t pts = 0;
	pts = m_stream->getPos();
	pts *= 1000;

	return pts;
}

bool RecordMediaPlayer::Seek(int64_t pts)
{
	if (!m_stream)
	{
		return false;
	}

	bool done = false;

	pts = pts / 1000;
	int ret = m_stream->seek(pts);
	if (ret == nvs::NVS_ERR_OK)
	{
		MDecode_ResetBuffer(m_handle);
		done = true;
	}
	return done;
}

int64_t RecordMediaPlayer::GetDuration()
{
	return m_duration * 1000;
}